package 个人密码库.view.inputview;


import 个人密码库.model.Password;

import java.util.Scanner;

public class ConsoleInput extends UserInput {
    private final Scanner scanner;

    public ConsoleInput(Scanner scanner) {
        this.scanner = scanner;
    }

    @Override
    public void input() {
        super.input();
    }

    public int getOperationNumber() {
        while (true) {
            try {
                System.out.print("请输入操作号（1-9）：");
                int result = Integer.parseInt(scanner.nextLine().trim());
                if (result > 0 && result <= 9) {
                    return result;
                } else {
                    printError();
                }
            } catch (NumberFormatException e) {
                printError();
            }
        }
    }

    private void printError() {
        System.out.println("您的输入有误！请重新输入！");
    }

    public String getKeyWord() {
        System.out.print("请输入关键字：");

        return scanner.nextLine();
    }

    // 获取Password对象，而不是密码字符串
    public Password getPassword() {
        Password password = new Password();
        password.setPassword(inputAPassword());
        password.setDescription(getDescription());

        return password;
    }

    public int getId() {
        while (true) {
            System.out.print("请输入要操作的密码的id：");
            try {
                return Integer.parseInt(scanner.nextLine().trim());
            } catch (NumberFormatException e) {
                printError();
            }
        }
    }

    // 获取一个密码字符串
    public String inputAPassword() {
        System.out.print("请输入密码：");

        return scanner.nextLine().trim();
    }

    public String getDescription() {
        System.out.print("请输入描述:");

        return scanner.nextLine();
    }

    public int[] getSomeIds() {
        System.out.print("请输入要操作的密码的id（如1, 2, 3）：");
        String input = scanner.nextLine().trim();
        String[] temp = input.split("\\D+");
        int[] result = new int[temp.length];

        for (int i = 0; i < temp.length; i++) {
            result[i] = Integer.parseInt(temp[i]);
        }

        return result;
    }

    public boolean askIfNeedToOperate(final String MESSAGE) {
        System.out.print(MESSAGE);
        String input = scanner.nextLine().trim();

        if ("yes".equalsIgnoreCase(input) || "y".equalsIgnoreCase(input)) {
            return true;
        } else if ("no".equalsIgnoreCase(input) || "n".equalsIgnoreCase(input)) {
            return false;
        }

        return false;
    }
}
